<template>
  <el-card class="custom-card">
    <!-- 返回按钮 -->
    <div class="flex justify-between items-start mb-6">
      <el-button
          type="text"
          class="back-button"
          @click="goBack"
      >
        <span class="text-lg">< 返回</span>
      </el-button>
    </div>

    <!-- 新闻标题 -->
    <h1 class="news-title">
      {{ currentNews?.title?.toUpperCase() }}
    </h1>

    <!-- 发布时间 -->
    <div class="publish-time">
      时间：{{ formatDateTime(currentNews?.publish_time) }}
    </div>

    <!-- 分隔线 -->
    <el-divider class="news-divider" />

    <!-- 第一段文字 -->
    <p class="news-paragraph">
      {{ currentNews?.paragraph1 }}
    </p>

    <!-- 新闻图片 -->
    <div class="news-image-container" v-if="currentNews?.image1">
      <el-image
          :src="fullImageUrl"
          fit="cover"
          class="news-image"
          :preview-src-list="[fullImageUrl]"
      />
    </div>

    <!-- 第二段文字 -->
    <p class="news-paragraph">
      {{ currentNews?.paragraph2 }}
    </p>
  </el-card>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useNewsStore } from '~/stores/newsStore'

const route = useRoute()
const router = useRouter()
const newsStore = useNewsStore()

// 获取当前新闻ID
const newsId = computed(() => route.query.id as string)



// 获取当前新闻数据
const currentNews = computed(() => {
  return newsStore.newsList.find(news => news.id === newsId.value)
})

// 格式化日期时间
const formatDateTime = (dateString?: string) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  }).replace(/\//g, '-')
}

const fullImageUrl = computed(() => {
  if (!currentNews.value?.image1) return ''
  // Add the prefix to the image path
  return `http://111.160.49.98:60/Species${currentNews.value.image1}`
})

// 返回按钮点击事件
const goBack = () => {
  router.push('/news/NewsCenter')
}

// 确保新闻数据已加载
onMounted(() => {
  if (newsStore.newsList.length === 0) {
    newsStore.fetchNews()
  }
})
</script>

<style scoped>
/* 自定义卡片样式 */
.custom-card {
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  padding: 2rem;
  transition: all 0.3s ease;
  max-width: 900px;
  margin: 0 auto;
}

.custom-card:hover {
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

/* 返回按钮样式 */
.back-button {
  padding: 0;
  color: #13675b;
  font-weight: bold;
}

.back-button:hover {
  color: #0e564c;
}

/* 新闻标题样式 */
.news-title {
  font-size: 1.8rem;
  font-weight: bold;
  color: #333;
  margin-bottom: 0.5rem;
  text-transform: uppercase;
}

/* 发布时间样式 */
.publish-time {
  font-size: 0.9rem;
  color: #666;
  margin-bottom: 1.5rem;
}

/* 分隔线样式 */
.news-divider {
  margin: 1.5rem 0;
  border-color: #e0e0e0;
}

/* 新闻段落样式 */
.news-paragraph {
  font-size: 1rem;
  line-height: 1.8;
  color: #333;
  margin-bottom: 1.5rem;
  text-align: justify;
}

/* 新闻图片样式 */
.news-image-container {
  margin: 2rem 0;
  border-radius: 8px;
  overflow: hidden;
}

.news-image {
  width: 100%;
  object-fit: cover;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .custom-card {
    padding: 1.5rem;
  }

  .news-title {
    font-size: 1.4rem;
  }

  .news-image {
    max-height: 300px;
  }
}
</style>